package com.hctforgreen.greenservice.ctr;

import java.util.ArrayList;

import android.database.Cursor;

import com.hctforgreen.greenservice.db.DbSearchKeywordAdapter;
import com.hctforgreen.greenservice.model.KeywordListEntity;
import com.hctforgreen.greenservice.model.KeywordListEntity.KeywordEntity;
import com.hctforgreen.greenservice.utils.HctConstants;

/**
 * db的控制器
 * 
 * @author Tallen Zhai
 * 
 */
public class DbKeywordController {

	private DbSearchKeywordAdapter mDbAdapter;

	
	public DbKeywordController(DbSearchKeywordAdapter dbAdapter) {
		mDbAdapter = dbAdapter;
	}

	// ============================
	// delete
	// ============================
	public boolean delete(KeywordEntity entity) {
		if (entity._id == 0) {
			entity._id = get_idFromDb(entity);
		}
		if (entity._id != -1) {
			String sql = "delete from " + HctConstants.DB_TABLE_NAME_KEYWORD_LST
					+ " where _id=?";
			mDbAdapter.execSQL(sql, new Object[] { entity._id });
			return true;
		}
		return false;
	}
	
	
	// ============================
	// Entity insert
	// ============================
	public boolean insert(KeywordEntity entity) {
		if (isExists(entity)) {
			return false;
		} else {
			String sql = "insert into "
					+ HctConstants.DB_TABLE_NAME_KEYWORD_LST 
					+ " (keyword)" 
					+ " values " 
					+ "(?);";
			mDbAdapter.execSQL(sql, new Object[] {
					entity.keyword
					});
			return true;
		}
	}
	
	
	// ============================
	// Entity isExists
	// ============================
	/**
	 * 判断此条KeywordEntity在db中是否已经存在
	 * 
	 * @param entity
	 * @return
	 */
	public boolean isExists(KeywordEntity entity) {
		String sql = "select count(*) from "
				+ HctConstants.DB_TABLE_NAME_KEYWORD_LST + " where keyword='"
				+ entity.keyword + "'";
		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		cursor.moveToFirst();
		if (cursor.getInt(0) > 0) {
			cursor.close();
			return true;
		} else { 
			cursor.close();
			return false;
		}
	}
	

	// ============================
	// Entity Lst
	// ============================
	
	/**
	 * 查询指定详情页的搜索历史列表
	 * @param seriesId
	 * @return
	 */
	public KeywordListEntity getKeywordListEntity(){
		
		Cursor cursor = getKeywordLstCursor();
		
		KeywordListEntity listEntity = new KeywordListEntity();
		for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
			KeywordEntity entity = new KeywordEntity();
			int _idIndex = cursor.getColumnIndex("_id");
			int keywordIndex = cursor.getColumnIndex("keyword");
			
			
			entity._id = cursor.getInt(_idIndex);
			entity.keyword = cursor.getString(keywordIndex);
			
			if(listEntity.dataList == null){
				listEntity.dataList = new ArrayList<KeywordEntity>();
			}
			listEntity.dataList.add(entity);
		}
		cursor.close();
		return listEntity;
	}
	

	// ============================
	// Cusrsor
	// ============================
	
	/**
	 * 查询Keywords,获取结果游标(Cursor), desc:最晚insert的会先被查询到
	 * 
	 * @return
	 */
	private Cursor getKeywordLstCursor() {
		
		String sql = "select * from " + HctConstants.DB_TABLE_NAME_KEYWORD_LST
					+ " order by _id desc";
		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		return cursor;
	}
	

	// ============================
	// get _id
	// ============================
	private int get_idFromDb(KeywordEntity entity) {
		String sql = "select _id from " + HctConstants.DB_TABLE_NAME_KEYWORD_LST
				+ " where " + "keyword='" + entity.keyword + "'";
		Cursor cursor = mDbAdapter.rawQuery(sql, null);
		cursor.moveToFirst();
		int _idIndex = cursor.getColumnIndex("_id");
		int _id = -1;
		try {
			_id = cursor.getInt(_idIndex);
			cursor.close();
		} catch (Exception e) {
			cursor.close();
		}
		return _id;
	}
	
}
